package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.myapplication.base.BaseActivity;

import java.io.File;

public class MainActivity extends BaseActivity {

    ArcProgressBar apb_space;
    ArcProgressBar apb_mem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        apb_space = findViewById(R.id.apb_space);
        apb_mem = findViewById(R.id.apb_mem);

        apb_space.setTitle("存储空间");
        apb_mem.setTitle("内存");

        apb_space.setTextColor(getResources().getColor(R.color.btn_bg_n));
        apb_mem.setTextColor(getResources().getColor(R.color.btn_bg_n));

//        apb_space.setProgress(50);
//        apb_mem.setProgress(40);




    }
    boolean needStop = false;
    @Override
    protected void onStart() {
        super.onStart();
        Log.e("", "onStart: ");
        needStop = false;
        getInfo();
        Log.e("", "开始刷新UI ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e("", "onStop: ");
        needStop = true;
    }

    void getInfo(){

        getSpace();
        getMem();

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (needStop){
                    Log.e("", "停止刷新UI ");
                    return;
                }
                getInfo();
            }
        },1000);
    }

    private void getMem() {
        ActivityManager service = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
        service.getMemoryInfo(info);
        long availMem = info.availMem;
        long totalMem = info.totalMem;

        long i = availMem * 100 / totalMem;
        apb_mem.setProgress((int) (100 - i));
    }

    private void getSpace() {
        File directory = Environment.getExternalStorageDirectory();
        StatFs statFs = new StatFs(directory.getPath());
        int availableBlocks = statFs.getAvailableBlocks();
        int blockCount = statFs.getBlockCount();
        int i = availableBlocks * 100 / blockCount;
        apb_space.setProgress(100 - i);
    }

    public void onClickCenterButton(View view) {
        if (view.getId() == R.id.rl_clear) {
            Toast.makeText(this, "clear", Toast.LENGTH_SHORT).show();
        }
        if (view.getId() == R.id.rl_saorao) {
            Toast.makeText(this, "saorao", Toast.LENGTH_SHORT).show();
        }
        if (view.getId() == R.id.rl_chasha) {
            Toast.makeText(this, "chasha", Toast.LENGTH_SHORT).show();
        }
        if (view.getId() == R.id.rl_guanli) {
//            Toast.makeText(this, "guanli", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this,ManagerListActivity.class));
        }
    }
}
